package _021_10_6

import (
	"sort"
	"strconv"
)

type ret struct {
	num int
	str string
}
type retSlice []ret

func (r retSlice) Len() int {
	return len(r)
}

func (r retSlice) Less(i, j int) bool {
	if r[i].num < r[j].num {
		return true
	}
	return false
}

func (r retSlice) Swap(i, j int) {
	temp := r[i]
	r[i] = r[j]
	r[j] = temp
}

func topKstrings(strings []string, k int) [][]string {
	// write code here

	resMap := make(map[string]int)
	for i := 0; i < len(strings); i++ {
		_, ok := resMap[strings[i]]
		if !ok {
			resMap[strings[i]] = 1
			continue
		}
		resMap[strings[i]] += 1
	}
	var result retSlice
	for s, i := range resMap {
		result = append(result, ret{i, s})
	}
	sort.Sort(result)
	d := make([][]string, 0)
	for i := 0; i < k; i++ {
		d = append(d, []string{result[i].str, strconv.Itoa(result[i].num)})
	}
	return d

}
